
using UnityEngine;

public class Com
{

    public static GameObject getCompoment(GameObject gameObject,params string[] str)
    {

        int len = str.Length;

        if (len <= 0)
        {
            return null;
        }
        else if (len == 1)
        {
            return gameObject.transform.Find(str[0]).gameObject;
        }
        else
        {
            GameObject tempObj = gameObject.transform.Find(str[0]).gameObject;

            for (int i = 1; i < len; i++)
            {
                tempObj = tempObj.transform.Find(str[i]).gameObject;
            }
            return tempObj;
        }


    }

    public static bool checkObjectTime(SystemUIEventScript obj, string name, float time)
    {
        if (obj.getVar(name).get_size() <= 0)
        {
            obj.getVar(name).clear_vector();
            obj.getVar(name).push_vector(obj.getObjectTime());
        }
        else
        {
            float encodeT = obj.getVar(name).get_vector(0);
            if (obj.getObjectTime() - encodeT > time)
            {
                obj.getVar(name).set_vector(0, obj.getObjectTime());
                return true;
            }
        }

        return false;
    }

    public static bool checkObjectTime(ActiveObject obj, string name, float time)
    {

        if (obj.getVar(name).get_size() <= 0)
        {
            obj.getVar(name).clear_vector();
            obj.getVar(name).push_vector(obj.getObjectTime() );
        }
        else
        {
            float encodeT = obj.getVar(name).get_vector(0);
            if (obj.getObjectTime() - encodeT > time)
            {
                obj.getVar(name).set_vector(0, obj.getObjectTime());
                return true;
            }
        }
      
        return false;

    }

    public static float getDistancePos(float point1, Direction direction, float offset)
    {
        if (direction == Direction.LEFT)
            return point1 - offset;
        else if (direction == Direction.RIGHT)
            return point1 + offset;
        return -1.0f;
    }


    public static float getUnformVelocity(float start, float center,float end,float minVar,float offset)
    {

        float rate = getRate(start,center,end);

        if (rate < 0.0f)
            rate = 0.0f;
        else if (rate > 1.0f)
            rate = 1.0f;

        return minVar + rate * (offset - minVar);

    }

    public static float getRate(float v1, float v2, float v3)
    {
        return (v2 - v1) / (v3 - v1);
    }

    public static Vector2 directionToVector2(Direction d)
    {
        if (d == Direction.LEFT)
            return Vector2.left;
        else if (d == Direction.RIGHT)
            return Vector2.right;
        return Vector2.zero;
    }

    public static float getDirectionToXPos(float x1, float x2)
    {
        if (x1 <= x2)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }

    public static Direction numToDirection(float v)
    {
        if (v == 0)
            return Direction.LEFT;
        else if (v == 1)
            return Direction.RIGHT;
        return Direction.ERROR;
    }
}